#include <stdio.h>
#include <string.h>

#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "hi_wifi_api.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#include "tcp.h"
#include "lwip/sockets.h"

#define WIFI_SSID   "helloworld"                // WIFI 名
#define WIFI_KEY    "helloworld1024"            // WIFI 密码
bool g_wifi_connect_flag = false;
static struct netif *g_lwip_netif = NULL;

/* 重置网络信息 */
void hi_sta_reset_addr(struct netif *pst_lwip_netif)
{
    ip4_addr_t st_gw;
    ip4_addr_t st_ipaddr;
    ip4_addr_t st_netmask;

    if (pst_lwip_netif == NULL) {
        printf("hisi_reset_addr::Null param of netdev\r\n");
        return;
    }

    /* 网关地址置0 */
    IP4_ADDR(&st_gw, 0, 0, 0, 0);
    /* IP地址置0 */
    IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
    /* 子网掩码置0 */
    IP4_ADDR(&st_netmask, 0, 0, 0, 0);
    /* 设置网络信息 */
    netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
}

/* 设置网络信息 */
void hi_sta_set_addr(struct netif *pst_lwip_netif)
{
    ip4_addr_t st_gw;
    ip4_addr_t st_ipaddr;
    ip4_addr_t st_netmask;

    if (pst_lwip_netif == NULL) {
        printf("hisi_reset_addr::Null param of netdev\r\n");
        return;
    }
    /* 设置网关地址 */
    IP4_ADDR(&st_gw, 192, 168, 1, 1);
    /* 设置IP地址 */
    IP4_ADDR(&st_ipaddr, 192, 168, 1, 100);
    /* 设置子网掩码 */
    IP4_ADDR(&st_netmask, 255, 255, 255, 0);
    /* 设置网络信息 */
    netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
}

/* 选择wifi事件 */
void wifi_wpa_event_cb(const hi_wifi_event *hisi_event)
{
    if (hisi_event == NULL)
        return;

    switch (hisi_event->event) {
        case HI_WIFI_EVT_SCAN_DONE:
            printf("WiFi: Scan results available\n");
            break;
        case HI_WIFI_EVT_CONNECTED:
            printf("WiFi: Connected\n");
            g_wifi_connect_flag = true;
            netifapi_dhcp_start(g_lwip_netif);
            break;
        case HI_WIFI_EVT_DISCONNECTED:
            printf("WiFi: Disconnected\n");
            netifapi_dhcp_stop(g_lwip_netif);
            hi_sta_reset_addr(g_lwip_netif);
            break;
        case HI_WIFI_EVT_WPS_TIMEOUT:
            printf("WiFi: wps is timeout\n");
            break;
        default:
            break;
    }
}

/* 扫描、连接指定AP */
int hi_wifi_start_connect(void)
{
    int ret;
    errno_t rc;
    hi_wifi_assoc_request assoc_req = {0};

    /* 设置要连接的WiFi的名字 */
    rc = memcpy_s(assoc_req.ssid, HI_WIFI_MAX_SSID_LEN + 1, WIFI_SSID, (sizeof(WIFI_SSID) - 1));
    if (rc != HISI_OK) {
        return -1;
    }

    /* 热点加密方式 */
    assoc_req.auth = HI_WIFI_SECURITY_WPA2PSK;

    /* 热点密码 */
    memcpy(assoc_req.key, WIFI_KEY, (sizeof(WIFI_KEY) - 1));

    /* 连接至热点 */
    ret = hi_wifi_sta_connect(&assoc_req);
    if (ret != HISI_OK) {
        return -1;
    }
    return 0;
}

/* 设置为STA模式 */
void wifi_sta_demo(void)
{
    hi_wifi_status *sta_status;
    int ret;
    char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
    int len = sizeof(ifname);
    unsigned int  num = WIFI_SCAN_AP_LIMIT;
    printf("start\n------------------------------------------------\n");
    /* 开启STA模式 */
    ret = hi_wifi_sta_start(ifname, &len);
    if (ret != HISI_OK) {
        return;
    }

    /* 注册回调函数 */
    ret = hi_wifi_register_event_callback(wifi_wpa_event_cb);
    if (ret != HISI_OK) {
        printf("register wifi event callback failed\n");
    }

    /* 为IP操作获取网络 */
    g_lwip_netif = netifapi_netif_find(ifname);
    if (g_lwip_netif == NULL) {
        printf("%s: get netif failed\n", __FUNCTION__);
        return ;
    }

    /* 扫描附件的热点 */
    ret = hi_wifi_sta_scan();
    if (ret != HISI_OK) {
        return ;
    }

    sleep(5);

    hi_wifi_ap_info *pst_results = malloc(sizeof(hi_wifi_ap_info) * WIFI_SCAN_AP_LIMIT);
    if (pst_results == NULL) {
        return ;
    }

    /* 获取扫描到的WiFi信息 */
    ret = hi_wifi_sta_scan_results(pst_results, &num);
    if (ret != HISI_OK) {
        free(pst_results);
        return ;
    }

    for (unsigned int loop = 0; (loop < num) && (loop < WIFI_SCAN_AP_LIMIT); loop++) {
        printf("SSID: %s\n", pst_results[loop].ssid);
    }
    /* 释放内存 */
    free(pst_results);

    /* 设置网络信息 */
    hi_sta_set_addr(g_lwip_netif);
    /* 连接到指定的热点 */
    ret = hi_wifi_start_connect();
    if (ret != 0) {
        return ;
    }

    return;

}

/** 让系统运行指定的函数 **/
SYS_RUN(wifi_sta_demo);